package cn.zebra.dev.shop.module.apply.controller;

import cn.zebra.dev.admin.module.admin.entity.AdminEntity;
import cn.zebra.dev.admin.module.admin.service.AdminService;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.member.module.member.entity.MemberEntity;
import cn.zebra.dev.member.module.member.service.MemberService;
import cn.zebra.dev.other.module.shopapply.entity.ShopApplyEntity;
import cn.zebra.dev.other.module.shopapply.service.ShopApplyService;
import cn.zebra.dev.shop.module.apply.dto.ShopApplyDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "店铺申请审核")
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private ShopApplyService shopApplyService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/list")
    @ApiOperation("店铺申请列表(可根据用户id,状态查询)")
    public Result<PageUtils<ShopApplyDTO>> list(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "state", required = false) Long state,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("state", state);
        params.put("userId", userId);
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<ShopApplyEntity> pageUtils = shopApplyService.findPage(params);
        PageUtils<ShopApplyDTO> data = new PageUtils<>(pageUtils);
        List<ShopApplyEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ShopApplyDTO> dtoList = new LinkedList<>();
            List<Long> memberIds = list.stream().map(ShopApplyEntity::getUserId).collect(Collectors.toList());
            List<MemberEntity> memberEntities = memberService.findByIds(memberIds);
            List<Long> adminIds = list.stream().map(ShopApplyEntity::getAdminId).collect(Collectors.toList());
            List<AdminEntity> adminEntities = adminService.findByIds(adminIds);
            list.forEach(entity -> {
                ShopApplyDTO dto = new ShopApplyDTO();
                BeanUtils.copyProperties(entity, dto);
                Optional<MemberEntity> memberEntityOptional = memberEntities.stream().filter(memberEntity -> memberEntity.getId().equals(entity.getUserId())).findAny();
                if (memberEntityOptional.isPresent()) {
                    MemberEntity memberEntity = memberEntityOptional.get();
                    dto.setUserName(memberEntity.getMemberName());
                }
                Optional<AdminEntity> adminEntityOptional = adminEntities.stream().filter(adminEntity -> adminEntity.getId().equals(entity.getAdminId())).findAny();
                if (memberEntityOptional.isPresent()) {
                    AdminEntity adminEntity = adminService.findById(entity.getAdminId());
                    dto.setAdminName(adminEntity.getAdminName());
                }

                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

}
